package com.mingtu.dzpatrol.utils;

import android.app.Activity;
import android.content.Context;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.TimeUtils;
import com.github.gzuliyujiang.calendarpicker.CalendarPicker;
import com.github.gzuliyujiang.calendarpicker.OnRangeDatePickListener;
import com.github.gzuliyujiang.calendarpicker.OnSingleDatePickListener;
import com.github.gzuliyujiang.calendarpicker.core.ColorScheme;
import com.github.gzuliyujiang.calendarpicker.core.DateUtils;
import com.github.gzuliyujiang.calendarpicker.core.FestivalProvider;
import com.mingtu.dzpatrol.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @ClassName :
 * @Author : wkk
 * @Date : 2023-9-25 11:22:59
 * @Email : 1281573890@qq.com
 * @Eescription :
 */
public class CalendarPickerUtils {

    private long startTimeInMillis, endTimeInMillis, singleTimeInMillis;
    private OnSureClickListener onSureClickListener = null;
    private OnSingleSureClickListener onSingleSureClickListener = null;
    private static CalendarPickerUtils calendarPickerUtils;
    private int minMonth = -60;
    private int maxMonth = 0;
    private int offsetMonth = 3;

    public static synchronized CalendarPickerUtils getInstance() {
        if (calendarPickerUtils == null) {
            calendarPickerUtils = new CalendarPickerUtils();
        }
        return calendarPickerUtils;

    }

    public void showPicker(Activity activity, Context context) {

        CalendarPicker picker = new CalendarPicker(activity);
        Date currentDate = new Date(System.currentTimeMillis());
        Calendar minCalendar = DateUtils.calendar(TimeUtils.getNowDate());
        minCalendar.add(Calendar.MONTH, getMinMonth());
        minCalendar.set(Calendar.DAY_OF_MONTH, DateUtils.maxDaysOfMonth(minCalendar.getTime()));
        Date minDate = minCalendar.getTime();
        Calendar maxCalendar = DateUtils.calendar(currentDate);
        maxCalendar.setTime(currentDate);
        maxCalendar.add(Calendar.MONTH, getMaxMonth());
        maxCalendar.set(Calendar.DAY_OF_MONTH, DateUtils.maxDaysOfMonth(maxCalendar.getTime()));
        Date maxDate = maxCalendar.getTime();
        picker.setRangeDate(minDate, maxDate);
        if (startTimeInMillis == 0 && endTimeInMillis == 0) {
            startTimeInMillis = currentDate.getTime() - 7 * android.text.format.DateUtils.DAY_IN_MILLIS;
            endTimeInMillis = currentDate.getTime();
        }
        picker.getOkView().setTextColor(context.getResources().getColor(R.color.colorPrimary));
        picker.setFestivalProvider(new MyFestivalProvider());
        picker.setColorScheme(new ColorScheme()
                .daySelectBackgroundColor(0xff79C057)
                .dayStressTextColor(0xff16b0ff));
        picker.setSelectedDate(startTimeInMillis, endTimeInMillis);
        picker.setOnRangeDatePickListener(new OnRangeDatePickListener() {
            @Override
            public void onRangeDatePicked(@NonNull Date startDate, @NonNull Date endDate) {
                startTimeInMillis = startDate.getTime();
                endTimeInMillis = endDate.getTime();

                Calendar startCalendar = Calendar.getInstance(); //Calendar类型的数据之后 将时间的时分秒数据重置为0,然后再转为Date型数据
                startCalendar.setTime(startDate);
                int year1 = startCalendar.get(Calendar.YEAR);
                int month1 = startCalendar.get(Calendar.MONTH);
                int day1 = startCalendar.get(Calendar.DAY_OF_MONTH);
                startCalendar.set(year1, month1, day1, 0, 0, 0);
                Date myStartDate = startCalendar.getTime();

                Calendar endCalendar = Calendar.getInstance();
                endCalendar.setTime(endDate);
                int year2 = endCalendar.get(Calendar.YEAR);
                int month2 = endCalendar.get(Calendar.MONTH);
                int day2 = endCalendar.get(Calendar.DAY_OF_MONTH);
                endCalendar.set(year2, month2, day2, 23, 59, 59);
                Date myEndDate = endCalendar.getTime();

                onSureClickListener.onRusult(myStartDate, myEndDate);
            }
        });
        picker.show();
    }

    public void showSinglePicker(Activity activity, Context context) {
        CalendarPicker picker = new CalendarPicker(activity);
        picker.setRangeDateOnFuture(getOffsetMonth());
        if (singleTimeInMillis == 0) {
            singleTimeInMillis = System.currentTimeMillis();
        }
        picker.setSelectedDate(singleTimeInMillis);
        picker.getOkView().setTextColor(context.getResources().getColor(R.color.colorPrimary));
        picker.setColorScheme(new ColorScheme()
                .daySelectBackgroundColor(0xff79C057)
                .dayStressTextColor(0xff16b0ff));
        picker.setFestivalProvider(new MyFestivalProvider());
        picker.setOnSingleDatePickListener(new OnSingleDatePickListener() {
            @Override
            public void onSingleDatePicked(@NonNull Date date) {
                singleTimeInMillis = date.getTime();

                Calendar singleCalendar = Calendar.getInstance(); //Calendar类型的数据之后 将时间的时分秒数据重置为0,然后再转为Date型数据
                singleCalendar.setTime(date);
                int year = singleCalendar.get(Calendar.YEAR);
                int month = singleCalendar.get(Calendar.MONTH);
                int day = singleCalendar.get(Calendar.DAY_OF_MONTH);
                singleCalendar.set(year, month, day, 23, 59, 59);
                Date myStartDate = singleCalendar.getTime();
                onSingleSureClickListener.onRusult(myStartDate);
            }
        });
        picker.show();
    }

    public long getStartTimeInMillis() {
        return startTimeInMillis;
    }

    public void setStartTimeInMillis(long startTimeInMillis) {
        this.startTimeInMillis = startTimeInMillis;
    }

    public long getEndTimeInMillis() {
        return endTimeInMillis;
    }

    public void setEndTimeInMillis(long endTimeInMillis) {
        this.endTimeInMillis = endTimeInMillis;
    }

    public void setOnSureClickListener(OnSureClickListener onSureClickListener) {
        this.onSureClickListener = onSureClickListener;
    }

    public void setOnSingleSureClickListener(OnSingleSureClickListener onSingleSureClickListener) {
        this.onSingleSureClickListener = onSingleSureClickListener;
    }

    public void resetData() {
        startTimeInMillis = 0;
        endTimeInMillis = 0;
        singleTimeInMillis = 0;
    }

    public int getMinMonth() {
        return minMonth;
    }

    public void setMinMonth(int minMonth) {
        this.minMonth = minMonth;
    }

    public int getMaxMonth() {
        return maxMonth;
    }

    public void setMaxMonth(int maxMonth) {
        this.maxMonth = maxMonth;
    }

    public int getOffsetMonth() {
        return offsetMonth;
    }

    public void setOffsetMonth(int offsetMonth) {
        this.offsetMonth = offsetMonth;
    }


    public interface OnSureClickListener {
        void onRusult(Date startDate, Date endDate);
    }

    public interface OnSingleSureClickListener {

        void onRusult(Date startDate);
    }

    private static class MyFestivalProvider implements FestivalProvider {
        @Override
        public String provideText(Date date) {
            String monthDay = new SimpleDateFormat("MMdd", Locale.PRC).format(date);
            switch (monthDay) {
                case "0101":
                    return "元旦节";
                case "0214":
                    return "情人节";
                case "0308":
                    return "妇女节";
                case "0312":
                    return "植树节";
                case "0401":
                    return "愚人节";
                case "0501":
                    return "劳动节";
                case "0504":
                    return "青年节";
                case "0601":
                    return "儿童节";
                case "0701":
                    return "建党节";
                case "0801":
                    return "建军节";
                case "0910":
                    return "教师节";
                case "1001":
                    return "国庆节";
                case "1111":
                    return "光棍节";
                case "1225":
                    return "圣诞节";
                default:
                    return "";
            }
        }
    }
}
